import logging
import os
import sys
from parsel import Selector
sys.path.append("..")
from MAPPING import list_file, str_con
from commens import dump_to_json, reALM, rmHTML



def map_table(trs):
    content = []
    ths = trs[0].xpath('th')
    thText = [rmHTML.sub('',x.get()).strip() for x in ths]
    logMAPPING.debug('有%d个表头'%len(thText))
    for tr in trs[1:]:
        argm = {}
        tds = tr.xpath('td')
        if len(tds)<len(thText):
            continue
        for i in range(len(thText)):
            argm[thText[i]] = rmHTML.sub('', tds[i].get())[0:-1].strip('\n\t')
        content.append(argm)
    return content


def parse_file(f):
    oneALM = {}
    html = Selector(f.read())
    ALM = html.xpath('/html/body/div[1]/h1/text()').get()
    logMAPPING.debug(ALM)
    ALM = reALM.search(ALM)
    if not ALM:
        return None,None
    ALM = ALM[0]
    sections = html.xpath('/html/body/div[1]/div/div')
    logMAPPING.debug("有%d个section" % len(sections))
    for s in sections:
        title = rmHTML.sub('',s.css('.sectiontitle').get())
        if title is None:
            continue
        if title == '告警属性' or title == '告警参数':
            trs = s.xpath('div//table//tr')
            if trs is not None:
                content = map_table(trs)
            else:
                content = None
        else:
            content = str_con(s.xpath('.//text()').getall()[1:])
        oneALM[title] = content
        logMAPPING.debug(title+"\t"+str(content))
    return ALM,oneALM


def map_file(pathBase, fileList):
    mmlDict = {}
    for i in range(0, len(fileList)):
        logMAPPING.debug('第%d个文件' % i)
        file = os.path.join(pathBase, fileList[i])
        with open(file, 'r') as f:
            k, v = parse_file(f)
            if k is None:
                print("无用文件：", fileList[i])
                continue
            elif k in mmlDict.keys():
                print("重复：", fileList[i])
                continue
            mmlDict[k] = v
            logMAPPING.debug(k, v)
            logMAPPING.debug('------------------------------------------------------------------------')
        # if i == 100:
        #     break
    return mmlDict


def main():
    suffix = '.html'
    pathBase = sys.argv[1]
    print(pathBase)
    fileList = list_file(pathBase, suffix)
    fileListSize = len(fileList)
    print("共找到%d个文件" % fileListSize)
    for i in range(0, 9 if 9 < fileListSize else fileListSize):
        print(fileList[i])
    print("...")
    ALMmap = map_file(pathBase, fileList)
    dump_to_json(ALMmap,'ALM_MAPPING.json')
    print('%d条数据已经转换为json' % len(ALMmap))


if __name__ == '__main__':
    logMAPPING = logging.getLogger("ALM_MAPPING")
    logMAPPING.setLevel(logging.DEBUG)
    hdMAPPING = logging.FileHandler("ALM_MAPPING.log", mode='w', encoding='utf-8')
    hdMAPPING.setLevel(logging.DEBUG)
    logMAPPING.addHandler(hdMAPPING)
    main()
